package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.SpecialColumnDao;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.SpecialColumnInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.SpecialColumnService;

@Service("specialColumnService")
public class SpecialColumnServiceImpl implements SpecialColumnService {

	@Resource(name = "specialColumnDao")
	private SpecialColumnDao specialColumnDao;

	@Override
	public SpecialColumnInfo queryByPrimartKey(String cid) {
		return specialColumnDao.getByKey(cid);
	}

	@Override
	public Pagination<SpecialColumnInfo> queryPage(SpecialColumnInfo specialInfo) {
		if (StringUtils.isNotEmpty(specialInfo.getOrderBy())
				&& specialInfo.getOrderType() != null) {
			if (specialInfo.getOrderType() == OrderType.asc) {
				specialInfo.setPage_orderByAsc(specialInfo.getOrderBy());

			} else {
				specialInfo.setPage_orderByDesc(specialInfo.getOrderBy());
			}
		}
		Integer count = specialColumnDao.totalCount(specialInfo);
		specialInfo.calculate();
		List<SpecialColumnInfo> list = specialColumnDao.queryList(specialInfo);
		Pagination<SpecialColumnInfo> page = specialColumnDao.queryPagination(count, list,
				specialInfo);
		return page;
	}

	@Override
	public int save(SpecialColumnInfo specialInfo, UserInfoVO currentUser) {
		specialInfo.setPublishTime(new Date());
		specialInfo.setUserId(currentUser.getUserId());
		specialInfo.setIsDelete(Constants.NOT_DELETED);
		return specialColumnDao.insert(specialInfo);
	}

	@Override
	public int update(SpecialColumnInfo specialInfo, UserInfoVO currentUser) {
		specialInfo.setUpdateTime(new Date());
		specialInfo.setUserId(currentUser.getUserId());
		return specialColumnDao.update(specialInfo);
	}

}
